<?php if(!defined('IN_FIRADIO')) {header('HTTP/1.1 403 Forbidden');exit('<h1>Access Denied</h1>');}?>
<?php $GLOBALS['script'][] = function() {?><script>
vue.data.disabled = false;
vue.data.button = {};
vue.data.button.down = false;
vue.data.button.del = false;
vue.data.api_recordcount = {};
vue.data.config = {};
vue.data.config.autoreload = {};
vue.data.config.autoreload.interval = 5000;
if (typeof(vue.watch) !== 'object') vue.watch = {};
vue.watch['config.autoreload.interval'] = function () {
    clearInterval(vue.data.config.autoreload.obj);
    if (vue.data.config.autoreload.interval > 0) {
        vue.data.config.autoreload.obj = setInterval(function () {
            vue.data.form.is_autoreload = 1;
            vue.methods.submit_bnb_recordcount();
        }, vue.data.config.autoreload.interval);
    }
};

var submit_after_result = function (that) {
    that.disabled = false;
};

vue.methods.formatBytes = window.formatBytes;
vue.methods.submit_bnb_recordcount = function () {
    const that = this;
    const form = window.clone(vue.data.form);
    vue.data.form.is_autoreload = 0;
    that.disabled = true;
    that.ApiPost('/panel/bnb/recordcount.php', form).then(function (data) {
        vue.data.api_recordcount = data.data;
        setTimeout(function() {
            if (jQuery("ul.nav-tabs").children(".active").length === 0) {
                jQuery("ul.nav-tabs li:first a").click();
            }
        }, 0);
    }, function (data) {
        console.log('reject:', data);
    }).catch(function (reason) {
        console.log('catch:', reason);
    })
};
vue.methods.submit_bnb_recordcount_mysql_to_redis = function () {
    if (!confirm("确定要重新统计状态的数量吗？")) return;
    const that = this;
    const form = {};
    that.ApiPost('/panel/bnb/recordcount_mysql_to_redis.php', form).then(function (data) {
        vue.methods.submit_bnb_recordcount();
    }, function (data) {
    })
};
vue.methods.getRecordCount = function (db, status) {
    const status_id = status['id'];
    const data = (typeof(db[status_id]) === 'object') ? db[status_id] : {};
    const count = (typeof(data.count) === 'number') ? data.count : 0;
    return count;
};
vue.methods.sum_db_status_list = function (db, list) {
    var ret = 0;
    for (var status_i in list) {
        const status_id = list[status_i];
        const data = (typeof(db[status_id]) === 'object') ? db[status_id] : {};
        const count = (typeof(data.count) === 'number') ? data.count : 0;
        ret += count;
    }
    return ret;
};
vue.methods.get_status_list = function (list) {
    var ret = [];
    for (var status_i in list) {
        const status = list[status_i];
        ret.push(status.id);
    }
    return ret;
};
vue.methods.sum_db_all = function (db) {
    var ret = 0;
    for (var status_id in db) {
        const data = (typeof(db[status_id]) === 'object') ? db[status_id] : {};
        const count = (typeof(data.count) === 'number') ? data.count : 0;
        ret += count;
    }
    return ret;
};
vue.methods.write_to_file = function (db, hostid, dbchar, status_id, downType) {
    if (downType == 2) {
        db[status_id].loading2 = true;
    } else {
        db[status_id].loading = true;
    }
    const that = this;
    //alert(hostid + '----' + dbchar  + '----' + status_id);
    const form = {};
    form.hostid = hostid;
    form.dbchar = dbchar;
    form.status_id = status_id;
    form.downType = downType;
    that.ApiPost('/panel/bnb/write_to_file.php', form).then(function (data) {
        vue.methods.submit_bnb_recordcount();
    }, function (data) {
        vue.methods.submit_bnb_recordcount();
        console.log('reject:', data);
    }).catch(function (reason) {
        vue.methods.submit_bnb_recordcount();
        console.log('catch:', reason);
    })
};
vue.methods.getFilesize = function (db, status_id, downType) {
    const data = (typeof(db[status_id]) === 'object') ? db[status_id] : {};
    return ((downType==2)?data.filesize2:data.filesize);
};
vue.methods.getFileDown = function (hostid, dbchar, status_id, downType) {
    const http_pre = '/api/download/';
    const filename_prefix = (downType == 2) ? 'bnb_record2_' : 'bnb_record_';
    return http_pre + '/' + filename_prefix + hostid + '_' + dbchar + '_' + status_id + '.txt';
};
vue.methods.delFile = function (hostid, dbchar, status_id, downType) {
    if (!confirm("确定要删除生成的文件吗？")) return;
    const that = this;
    //alert(hostid + '----' + dbchar  + '----' + status_id + '----' + downType);
    const form = {};
    form.hostid = hostid;
    form.dbchar = dbchar;
    form.status_id = status_id;
    form.downType = downType;
    that.ApiPost('/panel/bnb/download_file_del.php', form).then(function (data) {
        vue.methods.submit_bnb_recordcount();
    }, function (data) {
        vue.methods.submit_bnb_recordcount();
        console.log('reject:', data);
    }).catch(function (reason) {
        vue.methods.submit_bnb_recordcount();
        console.log('catch:', reason);
    })
};
vue.methods.delDb = function (hostid, dbchar, status_id, rcount) {
    if (!confirm("确定要从数据库中删除吗？")) return;
    const that = this;
    //alert(hostid + '----' + dbchar  + '----' + status_id + '----' + rcount);
    const form = {};
    form.hostid = hostid;
    form.dbchar = dbchar;
    form.status_id = status_id;
    form.rcount = rcount;
    that.ApiPost('/panel/bnb/record_del_db.php', form).then(function (data) {
        vue.methods.submit_bnb_recordcount();
    }, function (data) {
        vue.methods.submit_bnb_recordcount();
        console.log('reject:', data);
    }).catch(function (reason) {
        vue.methods.submit_bnb_recordcount();
        console.log('catch:', reason);
    })
};
vue.methods.status_config = function (dbchar) {
    if (dbchar !== 'a') dbchar = 'b';
    return this.api_recordcount.status_list[dbchar];
};
vue.mounted = function () {
    vue.methods.submit_bnb_recordcount();
    vue.watch['config.autoreload.interval']();
};
</script><?php }?>

<!-- Custom Tabs -->
<form v-on:submit="submit_bnb_recordcount" onsubmit="return false" :disabled="disabled">
<div class="nav-tabs-custom">
    
    <ul class="nav nav-tabs">
        <li v-for="(host,hostid) of api_recordcount.hosts" style="display: none;">
            <a :href="'#hostid_'+hostid" data-toggle="tab">{{hostid}}状态</a>
        </li>
        <li class="pull-left" style="font-size: 14px; padding: 8px;">
            <button class="btn btn-warning btn-xs" type="button" v-on:click="submit_bnb_recordcount_mysql_to_redis();">重新统计数量</button>
            <label style="margin: 0px 8px;"><input type="checkbox" v-model="button.down" />显示下载按钮</label>
            <label style="margin: 0px 8px;"><input type="checkbox" v-model="button.del" />显示删除按钮</label>
        </li>
        <li class="pull-right" style="font-size: 14px; padding: 8px;">
            <select v-model="config.autoreload.interval" style="margin-left: 10px;">
                <option value="1000">自动刷新/1s</option>
                <option value="3000">自动刷新/3s</option>
                <option value="5000">自动刷新/5s</option>
                <option value="0">手动刷新</option>
            </select>
            <a href="javascript:void(0)" v-on:click="form={};submit_bnb_recordcount();"
            class="fa fa-fw fa-refresh" style="font-size: 16px; display: inline;"></a>
        </li>
    </ul>

    <div class="tab-content">
        <div v-for="(host,hostid) of api_recordcount.hosts" class="tab-pane" :id="'hostid_'+hostid">
            <div class="row">
                <div class="col-xs-12 col-md-12 col-lg-12" v-for="(db,dbchar) of host">
                    <table class="table table-bordered table-striped dataTable">
                        <tr align="center">
                            <td :colspan="(status_config(dbchar).length+3)">
                                <div class="pull-left header">
                                    <i class="fa fa-th"></i>
                                    <font>{{dbchar.toLocaleUpperCase()}}数据统计</font>
                                </div>
                            </td>
                        </tr>
                        <tr align="center">
                            <td :width="(1/(status_config(dbchar).length+3)*100)+'%'">状态码</td>
                            <td :width="(1/(status_config(dbchar).length+3)*100)+'%'" v-for="(status) of status_config(dbchar)" :style="status.style">{{status.title}}</td>
                            <td :width="(1/(status_config(dbchar).length+3)*100)+'%'">合计</td>
                            <!--<td :width="(1/(status_config(dbchar).length+3)*100)+'%'">总共</td>-->
                            <td :width="(1/(status_config(dbchar).length+3)*100)+'%'">待处理</td>
                        </tr>
                        <tr align="center">
                            <td>总数</td>
                            <td v-for="(status) of status_config(dbchar)" :style="status.style">{{getRecordCount(db, status)}}</td>
                            <td>{{sum_db_status_list(db,get_status_list(status_config(dbchar)))}}</td>
                            <!--<td>{{sum_db_all(db)}}</td>-->
                            <td v-if="dbchar==='a'">{{sum_db_status_list(db,[0, 444, 450, 451, 452, 504])}}</td>
                            <td v-else>{{sum_db_status_list(db,[0, 444, 450, 451, 452, 453, 499, 504])}}</td>
                        </tr>
                        <tr align="center" v-if="button.down">
                            <td>普通下载</td>
                            <td v-for="(status) of status_config(dbchar)" :style="status.style">
                                <div v-if="getFilesize(db,status.id,1)">
                                    <a
                                        :href="getFileDown(hostid,dbchar,status.id,1)"
                                        target="_blank"
                                        download
                                    >
                                        {{formatBytes(getFilesize(db,status.id,1))}}
                                    </a>
                                    <a
                                    href="javascript:void(0)"
                                    v-on:click="delFile(hostid,dbchar,status.id,1)"
                                    style="color:red"
                                    >删</a>
                                </div>
                                <span v-else-if="getRecordCount(db, status)==0">-</span>
                                <span v-else-if="getRecordCount(db, status)>1000000">-</span>
                                <div v-else>
                                    <span v-if="db[status.id].loading">生成中..</span>
                                    <button
                                    type="button"
                                    v-on:click="write_to_file(db,hostid,dbchar,status.id)"
                                    v-else
                                    >生成</button>
                                </div>
                            </td>
                            <td>-</td>
                            <td>-</td>
                        </tr>
                        <tr align="center" v-if="button.down&&dbchar!='a'">
                            <td>关联下载</td>
                            <td v-for="(status) of status_config(dbchar)" :style="status.style">
                                <div v-if="getFilesize(db,status.id,2)">
                                    <a
                                        :href="getFileDown(hostid,dbchar,status.id,2)"
                                        target="_blank"
                                        download
                                    >
                                        {{formatBytes(getFilesize(db,status.id,2))}}
                                    </a>
                                    <a
                                    href="javascript:void(0)"
                                    v-on:click="delFile(hostid,dbchar,status.id,2)"
                                    style="color:red"
                                    >删</a>
                                </div>
                                <span v-else-if="getRecordCount(db, status)==0">-</span>
                                <span v-else-if="getRecordCount(db, status)>1000000">-</span>
                                <div v-else>
                                    <span v-if="db[status.id].loading2">生成中..</span>
                                    <button
                                    type="button"
                                    v-on:click="write_to_file(db,hostid,dbchar,status.id,2)"
                                    v-else
                                    >生成</button>
                                </div>
                            </td>
                            <td>-</td>
                            <td>-</td>
                        </tr>
                        <tr align="center" v-if="button.del">
                            <td>删除DB</td>
                            <td v-for="(status) of status_config(dbchar)" :style="status.style">
                                <span v-if="getRecordCount(db, status)==0">-</span>
                                <span v-else-if="getRecordCount(db, status)>1000000">-</span>
                                <span v-else-if="!getFilesize(db,status.id,1)">-</span>
                                <button v-else type="button"
                                v-on:click="delDb(hostid,dbchar,status.id,getRecordCount(db,status))"
                                style="color:red"
                                >删除</button>
                            </td>
                            <td>-</td>
                            <td>-</td>
                        </tr>
                    </table>

                </div>
            </div>
            <!-- /.tab-content -->
        </div>
        <!-- nav-tabs-custom -->
    </div>
    <!-- /.col -->

</div>
</form>
